package com.tencreat.service.mall.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.constant.MallConstants;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.utils.bean.BeanUtils;
import com.tencreat.domain.mall.MallAppCategory;
import com.tencreat.domain.mall.MallCategory;
import com.tencreat.domain.mall.vo.MallCategoryVO;
import com.tencreat.mapper.mall.MallCategoryMapper;
import com.tencreat.mapper.mall.MallGoodsMapper;
import com.tencreat.service.mall.IMallCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类目Service业务层处理
 *
 * @author tencreat
 * @date 2021-08-13
 */
@Service
public class MallCategoryServiceImpl extends ServiceImpl<MallCategoryMapper, MallCategory> implements IMallCategoryService {

    @Autowired
    private MallGoodsMapper mallGoodsMapper;

    /**
     * 查询类目
     *
     * @param id 类目ID
     * @return 类目
     */
    @Override
    public MallCategory selectMallCategoryById(ReqIdParam id) {
        return baseMapper.selectById(id.getId());
    }

    /**
     * 查询类目列表
     *
     * @param mallCategory 类目
     * @return 类目
     */
    @Override
    public List<MallCategory> selectMallCategoryList(MallCategory mallCategory) {
        return baseMapper.selectList(Wrappers.query(mallCategory));
    }

    /**
     * 新增商城类目
     *
     * @param mallCategory 类目
     * @return 结果
     */
    @Override
    public int insertMallCategory(MallCategory mallCategory) {
        Integer count = null;
        mallCategory.setCreateTime(LocalDateTime.now());
        if (MallConstants.MALL_CATEGORY_L2.equals(mallCategory.getLevel())) {
            MallCategory parent = baseMapper.selectById(mallCategory.getPid());
            if (!parent.getHasChildren()) {
                parent.setHasChildren(true);
                baseMapper.updateById(parent);
            }
            count = lambdaQuery().eq(MallCategory::getLevel, MallConstants.MALL_CATEGORY_L2).eq(MallCategory::getPid, mallCategory.getPid()).count();
        } else {
            count = lambdaQuery().eq(MallCategory::getLevel, MallConstants.MALL_CATEGORY_L1).count();
        }
        mallCategory.setSortOrder(count);
        mallCategory.setStatus(true);

        return baseMapper.insert(mallCategory);
    }


    /**
     * 修改类目
     *
     * @param mallCategory 类目
     * @return 结果
     */
    @Override
    public int updateMallCategory(MallCategory mallCategory) {
        //修改二级类目的父类目ID，需要修改是否有子节点字段
        MallCategory category = baseMapper.selectById(mallCategory.getId());
        if (!category.getPid().equals(mallCategory.getPid())) {
            Integer count = lambdaQuery().eq(MallCategory::getPid, category.getPid()).count();
            if (count < 2) {
                MallCategory pCategory = baseMapper.selectById(category.getPid());
                pCategory.setHasChildren(false);
                baseMapper.updateById(pCategory);
            }
            MallCategory newPcategory = baseMapper.selectById(mallCategory.getPid());
            newPcategory.setHasChildren(true);
            baseMapper.updateById(newPcategory);
        }
        //下架
        if (mallCategory.getStatus().equals(false) && mallCategory.getLevel().equals("L1")) {
            for (MallCategory mallCategory1 : lambdaQuery().eq(MallCategory::getPid, mallCategory.getId()).list()) {
                lambdaUpdate().set(MallCategory::getStatus, "0").eq(MallCategory::getId, mallCategory1.getId()).update();
                mallGoodsMapper.updateIsOnSaleByCategory("0", mallCategory1.getId());
            }
        }
        mallCategory.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(mallCategory);
    }

    /**
     * 批量删除类目
     *
     * @param ids 需要删除的类目ID
     * @return 结果
     */
    @Override
    public int deleteMallCategoryByIds(ReqIdsParam ids) {
        return baseMapper.deleteBatchIds(ids.getId());
    }

    /**
     * 删除类目信息
     *
     * @param id 类目ID
     * @return 结果
     */
    @Override
    public int deleteMallCategoryById(ReqIdParam id) {
        return baseMapper.deleteById(id.getId());
    }


    @Override
    public List<MallCategory> listPatent() {
        LambdaQueryWrapper<MallCategory> query = Wrappers.lambdaQuery(MallCategory.class);
        query.eq(MallCategory::getPid, 0)
                .eq(MallCategory::getCategoryType, 1)
//                .eq(MallCategory::getCategoryType,1)
                .orderByAsc(MallCategory::getSortOrder).orderByDesc(MallCategory::getSortUpdateTime);

        return baseMapper.selectList(query);
    }

    @Override
    public List<MallCategory> listStoreParent() {
        LambdaQueryWrapper<MallCategory> query = Wrappers.lambdaQuery(MallCategory.class);
        query.eq(MallCategory::getPid, 0)
                .eq(MallCategory::getCategoryType, 0)
                .orderByAsc(MallCategory::getSortOrder).orderByDesc(MallCategory::getSortUpdateTime);

        return baseMapper.selectList(query);
    }

    @Override
    public List<MallCategory> listChild(ReqIdParam reqIdParam) {
        LambdaQueryWrapper<MallCategory> query = Wrappers.lambdaQuery(MallCategory.class);
        query.eq(MallCategory::getPid, reqIdParam.getId()).eq(MallCategory::getCategoryType, 1);

//                .eq(MallCategory::getCategoryType,Long.valueOf(reqIdParam.getGoodsType()));
        query.orderByAsc(MallCategory::getSortOrder).orderByDesc(MallCategory::getSortUpdateTime);
        return baseMapper.selectList(query);
    }

    @Override
    public List<MallCategoryVO> apiListParent() {
        List<MallCategory> list = lambdaQuery().eq(MallCategory::getPid, 0).eq(MallCategory::getStatus, 1).eq(MallCategory::getCategoryType, 1)
                .orderByAsc(MallCategory::getSortOrder).orderByDesc(MallCategory::getSortUpdateTime)
                .list();
        return list.stream().map(item -> {
            MallCategoryVO mallCategoryVo = new MallCategoryVO();
            BeanUtils.copyProperties(item, mallCategoryVo);
            mallCategoryVo.setIconUrl(item.getIconUrl());
            return mallCategoryVo;
        }).collect(Collectors.toList());
    }


    @Override
    public List<MallCategoryVO> apiListChild(ReqIdParam idParam) {
        List<MallCategory> list = lambdaQuery().eq(MallCategory::getPid, idParam.getId()).eq(MallCategory::getStatus, 1).eq(MallCategory::getCategoryType, 1)
                .orderByAsc(MallCategory::getSortOrder).orderByDesc(MallCategory::getSortUpdateTime).list();
        return list.stream().map(item -> {
            MallCategoryVO mallCategoryVo = new MallCategoryVO();
            BeanUtils.copyProperties(item, mallCategoryVo);
            mallCategoryVo.setIconUrl(item.getIconUrl());
            return mallCategoryVo;
        }).collect(Collectors.toList());

    }

    /****
     * 上升
     * @param reqIdParam
     * @return
     */
    @Override
    public boolean reSortOrderReduce(MallCategory reqIdParam) {
        List<MallCategory> list = lambdaQuery().eq(MallCategory::getPid, reqIdParam.getPid()).orderByAsc(MallCategory::getSortOrder).orderByDesc(MallCategory::getSortUpdateTime).list();
        int indexOf = list.stream().map(MallCategory::getId).collect(Collectors.toList()).indexOf(reqIdParam.getId());
        if (indexOf == 0) {
            return true;
        }
        MallCategory orgCategory = list.get(indexOf);
        MallCategory oldCategory = list.get(indexOf - 1);
        Integer oldSortOrder = oldCategory.getSortOrder();
        Integer orgSortOrder = orgCategory.getSortOrder();
        orgCategory.setSortOrder(oldSortOrder);
        oldCategory.setSortOrder(orgSortOrder);
        updateById(orgCategory);
        updateById(oldCategory);
        return true;
    }

    /****
     * 下降
     * @param reqIdParam
     * @return
     */
    @Override
    public boolean reSortOrderPlus(MallCategory reqIdParam) {
        List<MallCategory> list = lambdaQuery().eq(MallCategory::getPid, reqIdParam.getPid()).orderByAsc(MallCategory::getSortOrder).orderByDesc(MallCategory::getSortUpdateTime).list();
        int indexOf = list.stream().map(MallCategory::getId).collect(Collectors.toList()).indexOf(reqIdParam.getId());
        if (indexOf == list.size() - 1) {
            return true;
        }
        MallCategory orgCategory = list.get(indexOf);
        MallCategory oldCategory = list.get(indexOf + 1);
        Integer oldSortOrder = oldCategory.getSortOrder();
        Integer orgSortOrder = orgCategory.getSortOrder();
        orgCategory.setSortOrder(oldSortOrder);
        oldCategory.setSortOrder(orgSortOrder);
        updateById(orgCategory);
        updateById(oldCategory);

        return true;
    }

    @Override
    public List<MallCategoryVO> listMedical() {
        List<MallCategory> list = lambdaQuery().eq(MallCategory::getStatus, 1).eq(MallCategory::getType, 1).eq(MallCategory::getCategoryType, 0).eq(MallCategory::getHasChildren, false)
                .orderByAsc(MallCategory::getSortOrder).orderByDesc(MallCategory::getSortUpdateTime).list();


        return list.stream().map(item -> {
            MallCategoryVO mallCategoryVo = new MallCategoryVO();
            BeanUtils.copyProperties(item, mallCategoryVo);
            mallCategoryVo.setIconUrl(item.getIconUrl());
            return mallCategoryVo;
        }).collect(Collectors.toList());

    }

    @Override
    public List<MallCategoryVO> listFoods() {
        List<MallCategory> list = lambdaQuery().eq(MallCategory::getStatus, 1).eq(MallCategory::getType, 0).eq(MallCategory::getCategoryType, 0).eq(MallCategory::getHasChildren, false)
                .orderByAsc(MallCategory::getSortOrder).orderByDesc(MallCategory::getSortUpdateTime).list();
        return list.stream().map(item -> {
            MallCategoryVO mallCategoryVo = new MallCategoryVO();
            BeanUtils.copyProperties(item, mallCategoryVo);
            mallCategoryVo.setIconUrl(item.getIconUrl());
            return mallCategoryVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<MallCategoryVO> listSettleIn(Long type) {
        List<MallCategory> list = lambdaQuery().eq(MallCategory::getStatus, 1).eq(MallCategory::getType, type).eq(MallCategory::getCategoryType, 0).eq(MallCategory::getHasChildren, false)
                .orderByAsc(MallCategory::getSortOrder).orderByDesc(MallCategory::getSortUpdateTime).list();
        return list.stream().map(item -> {
            MallCategoryVO mallCategoryVo = new MallCategoryVO();
            BeanUtils.copyProperties(item, mallCategoryVo);
            mallCategoryVo.setIconUrl(item.getIconUrl());
            return mallCategoryVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<MallCategory> listStoreChild(ReqIdParam reqIdParam) {
        LambdaQueryWrapper<MallCategory> query = Wrappers.lambdaQuery(MallCategory.class);
        query.eq(MallCategory::getPid, reqIdParam.getId()).eq(MallCategory::getCategoryType, 0);

//                .eq(MallCategory::getCategoryType,Long.valueOf(reqIdParam.getGoodsType()));
        query.orderByAsc(MallCategory::getSortOrder).orderByDesc(MallCategory::getSortUpdateTime);
        return baseMapper.selectList(query);
    }

    @Override
    public List<MallCategoryVO> listStoreOne() {
        List<MallCategory> list = lambdaQuery()
                .eq(MallCategory::getStatus, 1)
                .eq(MallCategory::getCategoryType, 0)
                .eq(MallCategory::getHasChildren, true)
                .eq(MallCategory::getPid, 0)
                .orderByAsc(MallCategory::getSortOrder)
                .orderByDesc(MallCategory::getSortUpdateTime).list();
        return list.stream().map(item -> {
            MallCategoryVO mallCategoryVo = new MallCategoryVO();
            BeanUtils.copyProperties(item, mallCategoryVo);
            mallCategoryVo.setIconUrl(item.getIconUrl());
            mallCategoryVo.setId(item.getId() == null ? 0L : item.getId());
            return mallCategoryVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<MallCategoryVO> listStoreTwo(ReqIdParam idParam) {
        List<MallCategory> list = lambdaQuery()
                .eq(MallCategory::getStatus, 1)
                .eq(MallCategory::getCategoryType, 0)
                .eq(MallCategory::getHasChildren, false)
                .eq(MallCategory::getPid,idParam.getId())
                .orderByAsc(MallCategory::getSortOrder)
                .orderByDesc(MallCategory::getSortUpdateTime).list();
        return list.stream().map(item -> {
            MallCategoryVO mallCategoryVo = new MallCategoryVO();
            BeanUtils.copyProperties(item, mallCategoryVo);
            mallCategoryVo.setIconUrl(item.getIconUrl());
            mallCategoryVo.setId(item.getId() == null ? 0L : item.getId());
            return mallCategoryVo;
        }).collect(Collectors.toList());
    }
}
